import java.util.Arrays;

public class Solution1286 {

    char[] chars;
    char[] target;

    public Solution1286(String characters, int combinationLength) {
        target=characters.toCharArray();
        Arrays.sort(target);
        chars=new char[combinationLength];
        System.arraycopy(target, 0, chars, 0, combinationLength);
    }

    public String next() {
        String ans= String.valueOf(chars);
        boolean flag=true;
        for (int i=chars.length-1;i>=0;i--){
            if (chars[i]!=target[target.length-chars.length+i]){
                int j=0;
                for (int k=0;k<target.length;k++){
                    if (target[k]==chars[i]){
                        j=k;
                        break;
                    }
                }
                System.arraycopy(target, j + i - i + 1, chars, i, chars.length - i);
                flag=false;
                break;
            }
        }
        if (flag){
            chars=null;
        }
        return ans;
    }

    public boolean hasNext() {
        return chars!=null;
    }

    public static void main(String[] args) {
        Solution1286 solution1286=new Solution1286("abc",2);
        System.out.println(solution1286.next());
        System.out.println(solution1286.hasNext());
        System.out.println(solution1286.next());
        System.out.println(solution1286.hasNext());
        System.out.println(solution1286.next());
        System.out.println(solution1286.hasNext());
    }
}
